<?php

namespace app\index\controller;

use app\Model\Container;
use app\Model\WxMessage;
use EasyWeChat\Kernel\Messages\Article;
use EasyWeChat\Kernel\Messages\Media;
use EasyWeChat\Kernel\Messages\News;
use EasyWeChat\Kernel\Messages\NewsItem;
use EasyWeChat\Kernel\Messages\Text;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\Transfer;
use think\Controller;

class Wxtest extends Controller
{
    public $config;


    public function test()
    {
        //依赖注入和IOC容器实现
        $container = new Container();
        $container->bind('WxMessage',function ($container , $moduleName){
            return new WxMessage($container->make($moduleName));
        });

        $container->bind('Text',function ($container){
           return new \app\Model\Text();
        });

        $message = $container->make('WxMessage',['Text']);
        $message->app->xml()->send();
    }

    public function __construct()
    {
        $this->config = [
            'app_id' => 'wxa4a086eee491c75e',
            'secret' => 'eb18c6897c0a5cbd1047a33693888ec9',

            'response_type' => 'array',

            'log' => [
                'level' => 'debug',
                'file' => __DIR__.'/wechat.log',
            ],
        ];
    }

    public function index()
    {
        if (isset($_GET["echostr"])) {
            return $this->validation();
        }else{
            return $this->reply();
        }


    }


    private function validation()
    {
        $nonce     = $_GET['nonce'];
        $token     = 'utioerlfglcvmbvlkfjgd';
        $timestamp = $_GET['timestamp'];
        $echostr   = $_GET['echostr'];
        $signature = $_GET['signature'];
        //形成数组，然后按字典序排序
        $array = array();
        $array = array($nonce, $timestamp, $token);
        sort($array);
        //拼接成字符串,sha1加密 ，然后与signature进行校验
        $str = sha1( implode( $array ) );
        if( $str == $signature && $echostr ){
            //第一次接入weixin api接口的时候
            echo  $echostr;
            exit;
        }
    }


    private function reply()
    {
        $app = Factory::officialAccount($this->config);

        $app->server->push(function ($message) use ($app){
            switch ($message['MsgType']) {
                case 'event':
                    return '收到事件消息';
                    break;
                case 'text':
                    $media = new Media('OpUREpol33tYb8vxjVKk1zHIkki-ng00ZiQWqQrJ3aU', 'mpnews');
                    $result = $app->customer_service->message($media)->to('om-6EwW9cpnlWHxHkJ-n9rSdSV6c')->send();
                    break;
                default:
                    return '收到其它消息';
                    break;
            }

        });


        $response = $app->server->serve();
        // 将响应输出
        $response->send(); // Laravel 里请使用：return $response;
    }


    public function material()
    {
        $article1 = [
            'title' => '测试文章1',
            'author' => '超大大',
            'content' => '超大大的测试文章1',
            'thumb_media_id' => 'OpUREpol33tYb8vxjVKk13Z-R9y0w7FG3npWK6geA2g',
            'digest' => '超大大的测试文章1',
            'source_url' => 'http://www.baidu.com',
            'show_cover' => '1',
            'need_open_comment' => '0',
            'only_fans_can_comment' => '1',
        ];
        $article2 = [
            'title' => '测试文章2',
            'author' => '超大大',
            'content' => '超大大的测试文章1',
            'thumb_media_id' => 'OpUREpol33tYb8vxjVKk13Z-R9y0w7FG3npWK6geA2g',
            'digest' => '超大大的测试文章1',
            'source_url' => 'http://www.baidu.com',
            'show_cover' => '1',
            'need_open_comment' => '1',
            'only_fans_can_comment' => '0',
        ];
        $article3 = [
            'title' => '测试文章3',
            'author' => '超大大',
            'content' => '超大大的测试文章1',
            'thumb_media_id' => 'OpUREpol33tYb8vxjVKk1_nt98imKfENghzUtt5rd5k',
            'digest' => '超大大的测试文章1',
            'source_url' => 'http://www.baidu.com',
            'show_cover' => '1',
            'need_open_comment' => '1',
            'only_fans_can_comment' => '1',
        ];
        $app = Factory::officialAccount($this->config);
        $res = $app->material->uploadArticle([$article1,$article2,$article3]);

        dump($res);
    }

    public function get()
    {
        $app = Factory::officialAccount($this->config);
        $resource = $app->material->get('OpUREpol33tYb8vxjVKk1zHIkki-ng00ZiQWqQrJ3aU');
        halt($resource);
    }


    public function send()
    {
        $mediaId = 'OpUREpol33tYb8vxjVKk11KypwYdD_1FgObg8mMzJSc';
        $media = new Media($mediaId, 'mpnews');
        $app = Factory::officialAccount($this->config);
        $result = $app->customer_service->message($media)->to('om-6EwW9cpnlWHxHkJ-n9rSdSV6c')->send();
    }


    public function upload()
    {
        ini_set("display_errors", "On");

        error_reporting(E_ALL | E_STRICT);

        $app = Factory::officialAccount($this->config);
        $result = $app->material->uploadImage(ROOT_PATH . "public/f9d212ffba7d5d52552fa0981f7b2518.jpg");

        dump($result);
    }


    public function openMsg()
    {
        $app = Factory::officialAccount($this->config);
        $res = $app->comment->open('6557220115628848466', $index = 1);
    }


    /*$items = [
                        new NewsItem([
                            'title'       => '123',
                            'description' => '123',
                            'url'         => 'http://www.baidu.com',
                            'image'       => 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1193474556,1223904415&fm=27&gp=0.jpg',
                            // ...
                        ]),
                        new NewsItem([
                            'title'       => '456',
                            'description' => '456',
                            'url'         => 'http://www.baidu.com',
                            'image'       => 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1193474556,1223904415&fm=27&gp=0.jpg',
                            // ...
                        ]),
                        new NewsItem([
                            'title'       => '789',
                            'description' => '789',
                            'url'         => 'http://www.baidu.com',
                            'image'       => 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1193474556,1223904415&fm=27&gp=0.jpg',
                            // ...
                        ]),
                        // ...
                    ];
                    return $news = new News($items);*/
}